package org.sith.taskrunner.listeners;

import org.sith.taskrunner.ejb.*;
import org.sith.taskrunner.ejb.exceptions.TaskToolkitRegisterException;
import org.sith.taskrunner.ejb.services.TaskToolkitRegisterService;

import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Map;

public class TaskServletContextListener implements ServletContextListener {

    private TasksManager taskManager;

    @EJB(name = "TaskToolkitRegisterServiceEJB")
    private TaskToolkitRegisterService toolkitRegisterService;

   
    @Override
    public void contextDestroyed(ServletContextEvent contextEvent) {
    }

    @Override
    public void contextInitialized(ServletContextEvent contextEvent) {


        taskManager = TasksManager.getInstance();
        taskManager.addTask("Sum", "/core/tasks/sumtask");
        taskManager.addTask("QuadraticEquation", "/core/tasks/equationtask");
        ServletContext context = contextEvent.getServletContext();
        context.setAttribute("TasksManager", taskManager);


        TaskManagerHelper instance = TaskManagerHelper.getInstance();
        instance.addToolkit("sum", new SumTaskToolkit());


        ToolkitXMLLoader loader = new ToolkitXMLLoader();
        Map<String, String> toolkitsMap = loader.load();
       /* for (String toolkitName : toolkitsMap.keySet()) {
            try {
                UserThreadKeeper.setSystemUser();
                toolkitRegisterService.registerToolkitBuilder(taskToolkit);
                toolkitRegisterService.activateToolkitBuilder(toolkitName);
            } catch (TaskToolkitRegisterException e) {
                throw new IllegalStateException(e);
            } finally {
                UserThreadKeeper.clear();
            }
        }*/


    }

}
